function [ new_individual ] = apply_backpropagation( individual )
% BACK_PROPAGATION
%   Este algoritmo le aplica back-propagation a la poblacion y devuelve la
%   poblacion actualizada a partir del uso de este algoritmo.

global LAYERS;

p = get_patterns();
patterns = p(1:3,:);
S_patterns = p(4,:);

weights = matrixes_from_vector(individual);
for i = 1:length(patterns)
    ret = feed_forward(weights, patterns(:,i), S_patterns(i));
    deltas = ret{1};
    h = ret{2};
    delta_weights = back_propagation(weights, deltas, h, patterns(:,i));
    for j = 1:length(LAYERS)
        weights{j} = weights{j} + delta_weights{j};
        if i > 1
            weights{j} = weights{j} + 0.1 * previous_delta_weights{j};
        end
    end
    previous_delta_weights = delta_weights;
end
new_individual = individual_from_matrixes(weights);

end

